home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / os / sprite / RCS / access.c,v < prev    next >
Encoding:
Text File  |  1992-01-25  |  19.0 KB  |  867 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     92.01.24.17.40.54;  author stolcke;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.02.14.19.25.13;  author tve;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @Original X11R4 distribution
  22. @
  23.  
  24.  
  25. 1.2
  26. log
  27. @authorize pdev connections as local connections
  28. @
  29. text
  30. @/***********************************************************
  31. Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts,
  32. and the Massachusetts Institute of Technology, Cambridge, Massachusetts.
  33.  
  34.                         All Rights Reserved
  35.  
  36. Permission to use, copy, modify, and distribute this software and its 
  37. documentation for any purpose and without fee is hereby granted, 
  38. provided that the above copyright notice appear in all copies and that
  39. both that copyright notice and this permission notice appear in 
  40. supporting documentation, and that the names of Digital or MIT not be
  41. used in advertising or publicity pertaining to distribution of the
  42. software without specific, written prior permission.  
  43.  
  44. DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  45. ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  46. DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  47. ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  48. WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  49. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  50. SOFTWARE.
  51.  
  52. ******************************************************************/
  53.  
  54. /* $XConsortium: access.c,v 1.44 89/11/12 15:38:59 rws Exp $ */
  55.  
  56. #include "Xos.h"
  57. #include "X.h"
  58. #include "Xproto.h"
  59. #include "misc.h"
  60. #include "site.h"
  61. #include <errno.h>
  62. #include <sys/socket.h>
  63. #include <sys/ioctl.h>
  64.  
  65. #ifdef hpux
  66. # include <sys/utsname.h>
  67. # ifdef HAS_IFREQ
  68. #  include <net/if.h>
  69. # endif
  70. #else
  71. # include <net/if.h>
  72. #endif /* hpux */
  73.  
  74. #include <netdb.h>
  75. #ifdef TCPCONN
  76. #include <netinet/in.h>
  77. #endif /* TCPCONN */
  78. #ifdef DNETCONN
  79. #include <netdnet/dn.h>
  80. #include <netdnet/dnetdb.h>
  81. #endif
  82. #undef NULL
  83. #include <stdio.h>
  84. #include "dixstruct.h"
  85. #include "osdep.h"
  86.  
  87. #define acmp(a1, a2, len) bcmp((char *)(a1), (char *)(a2), len)
  88. #define acopy(a1, a2, len) bcopy((char *)(a1), (char *)(a2), len)
  89. #define addrEqual(fam, address, length, host) \
  90.              ((fam) == (host)->family &&\
  91.               (length) == (host)->len &&\
  92.               !acmp (address, (host)->addr, length))
  93.  
  94. #ifdef hpux
  95. #define getpeername(fd, from, fromlen)    hpux_getpeername(fd, from, fromlen)
  96. #endif
  97.  
  98. extern char    *index();
  99.  
  100. static int XFamily(), UnixFamily();
  101. static int ConvertAddr(), CheckAddr();
  102. static void NewHost();
  103.  
  104. typedef struct _host {
  105.     short        family;
  106.     short        len;
  107. #ifdef DNETCONN
  108.     unsigned char    addr[sizeof(struct dn_naddr)];
  109. #else
  110.     unsigned char    addr[4];    /* will need to be bigger eventually */
  111. #endif
  112.     struct _host *next;
  113. } HOST;
  114.  
  115. static HOST *selfhosts = NULL;
  116. static HOST *validhosts = NULL;
  117. static int AccessEnabled = DEFAULT_ACCESS_CONTROL;
  118. static int LocalHostEnabled = FALSE;
  119. static int UsingXdmcp = FALSE;
  120.  
  121. typedef struct {
  122.     int af, xf;
  123. } FamilyMap;
  124.  
  125. static FamilyMap familyMap[] = {
  126. #ifdef     AF_DECnet
  127. #ifdef     DNETCONN
  128.     {AF_DECnet, FamilyDECnet},
  129. #endif
  130. #endif /* AF_DECnet */
  131. #ifdef     AF_CHAOS
  132. #ifdef     CHAOSCONN
  133.     {AF_CHAOS, FamilyChaos},
  134. #endif
  135. #endif /* AF_CHAOS */
  136. #ifdef    AF_INET
  137. #ifdef    TCPCONN
  138.     {AF_INET, FamilyInternet}
  139. #endif
  140. #endif
  141. };
  142.  
  143. /*
  144.  * called when authorization is not enabled to add the
  145.  * local host to the access list
  146.  */
  147.  
  148. EnableLocalHost ()
  149. {
  150.     if (!UsingXdmcp)
  151.     {
  152.     LocalHostEnabled = TRUE;
  153.     AddLocalHosts ();
  154.     }
  155. }
  156.  
  157. /*
  158.  * called at init time when XDMCP will be used; xdmcp always
  159.  * adds local hosts manually when needed
  160.  */
  161.  
  162. AccessUsingXdmcp ()
  163. {
  164.     UsingXdmcp = TRUE;
  165.     LocalHostEnabled = FALSE;
  166. }
  167.  
  168. #define FAMILIES ((sizeof familyMap)/(sizeof familyMap[0]))
  169.  
  170. #if defined (hpux) && ! defined (HAS_IFREQ)
  171. /* Define this host for access control.  Find all the hosts the OS knows about 
  172.  * for this fd and add them to the selfhosts list.
  173.  * HPUX version - hpux does not have SIOCGIFCONF ioctl;
  174.  */
  175. DefineSelf (fd)
  176.     int fd;
  177. {
  178.     register int n;
  179.     int    len;
  180.     caddr_t    addr;
  181.     int        family;
  182.     register HOST    *host;
  183.  
  184.     struct utsname name;
  185.     register struct hostent  *hp;
  186.  
  187.     union {
  188.     struct  sockaddr   sa;
  189.     struct  sockaddr_in  in;
  190.     } saddr;
  191.     
  192.     struct    sockaddr_in    *inetaddr;
  193.  
  194.     /* Why not use gethostname()?  Well, at least on my system, I've had to
  195.      * make an ugly kernel patch to get a name longer than 8 characters, and
  196.      * uname() lets me access to the whole string (it smashes release, you
  197.      * see), whereas gethostname() kindly truncates it for me.
  198.      */
  199.     uname(&name);
  200.     hp = gethostbyname (name.nodename);
  201.     if (hp != NULL)
  202.     {
  203.     saddr.sa.sa_family = hp->h_addrtype;
  204.     inetaddr = (struct sockaddr_in *) (&(saddr.sa));
  205.     acopy ( hp->h_addr, &(inetaddr->sin_addr), hp->h_length);
  206.     len = sizeof(saddr.sa);
  207.     family = ConvertAddr ( &(saddr.sa), &len, &addr);
  208.     if ( family > 0)
  209.     {
  210.         for (host = selfhosts;
  211.          host && !addrEqual (family, addr, len, host);
  212.          host = host->next) ;
  213.         if (!host)
  214.         {
  215.         /* add this host to the host list.    */
  216.         host = (HOST *) xalloc (sizeof (HOST));
  217.         if (host)
  218.         {
  219.             host->family = family;
  220.             host->len = len;
  221.             acopy ( addr, host->addr, len);
  222.             host->next = selfhosts;
  223.             selfhosts = host;
  224.         }
  225.         }
  226.     }
  227.     }
  228. }
  229.  
  230. #else
  231. /* Define this host for access control.  Find all the hosts the OS knows about 
  232.  * for this fd and add them to the selfhosts list.
  233.  */
  234. DefineSelf (fd)
  235.     int fd;
  236. {
  237.     char        buf[2048];
  238.     struct ifconf    ifc;
  239.     register int    n;
  240.     int         len;
  241.     pointer         addr;
  242.     int         family;
  243.     register HOST     *host;
  244.     register struct ifreq *ifr;
  245.     
  246. #ifdef DNETCONN
  247.     struct dn_naddr *dnaddr = getnodeadd(0);
  248.     /*
  249.      * AF_DECnet may not be listed in the interface list.  Instead use
  250.      * the supported library call to find out the local address (if any).
  251.      */
  252.     if (dnaddr)
  253.     {    
  254.     addr = (pointer) dnaddr;
  255.     len = dnaddr->a_len + sizeof(dnaddr->a_len);
  256.     family = AF_DECnet;
  257.     for (host = selfhosts;
  258.          host && !addrEqual (family, addr, len, host);
  259.          host = host->next)
  260.         ;
  261.         if (!host)
  262.     {
  263.         host = (HOST *) xalloc (sizeof (HOST));
  264.         if (host)
  265.         {
  266.         host->family = family;
  267.         host->len = len;
  268.         acopy(addr, host->addr, len);
  269.         host->next = selfhosts;
  270.         selfhosts = host;
  271.         }
  272.     }
  273.     }
  274. #endif
  275.     ifc.ifc_len = sizeof (buf);
  276.     ifc.ifc_buf = buf;
  277.     if (ioctl (fd, (int) SIOCGIFCONF, (pointer) &ifc) < 0)
  278.         Error ("Getting interface configuration");
  279.     for (ifr = ifc.ifc_req, n = ifc.ifc_len / sizeof (struct ifreq); --n >= 0;
  280.      ifr++)
  281.     {
  282.     len = sizeof(ifr->ifr_addr);
  283. #ifdef DNETCONN
  284.     /*
  285.      * DECnet was handled up above.
  286.      */
  287.     if (ifr->ifr_addr.sa_family == AF_DECnet)
  288.         continue;
  289. #endif /* DNETCONN */
  290.         if ((family = ConvertAddr (&ifr->ifr_addr, &len, &addr)) <= 0)
  291.         continue;
  292.         for (host = selfhosts;
  293.           host && !addrEqual (family, addr, len, host);
  294.          host = host->next)
  295.         ;
  296.         if (host)
  297.         continue;
  298.         host = (HOST *) xalloc (sizeof (HOST));
  299.     if (host)
  300.     {
  301.         host->family = family;
  302.         host->len = len;
  303.         acopy(addr, host->addr, len);
  304.         host->next = selfhosts;
  305.         selfhosts = host;
  306.     }
  307. #ifdef XDMCP
  308.     {
  309.         struct sockaddr broad_addr;
  310.  
  311.         /*
  312.          * If this isn't an Internet Address, don't register it.
  313.          */
  314.         if (family != AF_INET)
  315.         continue;
  316.  
  317.         XdmcpRegisterConnection (FamilyInternet, (char *)addr, len);
  318.         broad_addr = ifr->ifr_addr;
  319.         ((struct sockaddr_in *) &broad_addr)->sin_addr.s_addr =
  320.         htonl (INADDR_BROADCAST);
  321. #ifdef SIOCGIFBRDADDR
  322.         {
  323.             struct ifreq    broad_req;
  324.     
  325.             broad_req = *ifr;
  326.         if (ioctl (fd, SIOCGIFFLAGS, (char *) &broad_req) != -1 &&
  327.             (broad_req.ifr_flags & IFF_BROADCAST) &&
  328.             (broad_req.ifr_flags & IFF_UP)
  329.             )
  330.         {
  331.             broad_req = *ifr;
  332.             if (ioctl (fd, SIOCGIFBRDADDR, &broad_req) != -1)
  333.             broad_addr = broad_req.ifr_addr;
  334.             else
  335.             continue;
  336.         }
  337.         else
  338.             continue;
  339.         }
  340. #endif
  341.         XdmcpRegisterBroadcastAddress ((struct sockaddr_in *) &broad_addr);
  342.     }
  343. #endif
  344.     }
  345. }
  346. #endif /* hpux && !HAS_IFREQ */
  347.  
  348. #ifdef XDMCP
  349. void
  350. AugmentSelf(fd)
  351.     int fd;
  352. {
  353.     int len;
  354.     struct sockaddr from;
  355.     int family;
  356.     pointer addr;
  357.     register HOST *host;
  358.  
  359.     len = sizeof(from);
  360.     if (getpeername(fd, &from, &len))
  361.     return;
  362.     family = ConvertAddr(&from, &len, &addr);
  363.     if (family <= 0)
  364.     return;
  365.     for (host = selfhosts; host; host = host->next)
  366.     {
  367.     if (addrEqual(family, addr, len, host))
  368.         return;
  369.     }
  370.     host = (HOST *)xalloc(sizeof(HOST));
  371.     if (!host)
  372.     return;
  373.     host->family = family;
  374.     host->len = len;
  375.     acopy(addr, host->addr, len);
  376.     host->next = selfhosts;
  377.     selfhosts = host;
  378. }
  379. #endif
  380.  
  381. AddLocalHosts ()
  382. {
  383.     HOST    *self;
  384.  
  385.     for (self = selfhosts; self; self = self->next)
  386.     NewHost (self->family, self->addr, self->len);
  387. }
  388.  
  389. /* Reset access control list to initial hosts */
  390. ResetHosts (display)
  391.     char *display;
  392. {
  393.     register HOST    *host;
  394.     char         hostname[120];
  395.     char        fname[32];
  396.     FILE        *fd;
  397.     char        *ptr;
  398.     union {
  399.         struct sockaddr    sa;
  400. #ifdef TCPCONN
  401.         struct sockaddr_in in;
  402. #endif /* TCPCONN */
  403. #ifdef DNETCONN
  404.         struct sockaddr_dn dn;
  405. #endif
  406.     }             saddr;
  407. #ifdef DNETCONN
  408.     struct nodeent     *np;
  409.     struct dn_naddr     dnaddr, *dnaddrp, *dnet_addr();
  410. #endif
  411.     int            family;
  412.     int            len;
  413.     pointer        addr;
  414.     register struct hostent *hp;
  415.  
  416.     AccessEnabled = DEFAULT_ACCESS_CONTROL;
  417.     LocalHostEnabled = FALSE;
  418.     while (host = validhosts)
  419.     {
  420.         validhosts = host->next;
  421.         xfree (host);
  422.     }
  423.     strcpy (fname, "/etc/X");
  424.     strcat (fname, display);
  425.     strcat (fname, ".hosts");
  426.     if (fd = fopen (fname, "r")) 
  427.     {
  428.         while (fgets (hostname, sizeof (hostname), fd))
  429.     {
  430.         if (ptr = index (hostname, '\n'))
  431.             *ptr = 0;
  432. #ifdef DNETCONN
  433.         if ((ptr = index (hostname, ':')) && (*(ptr + 1) == ':'))
  434.     {
  435.             /* node name (DECnet names end in "::") */
  436.             *ptr = 0;
  437.         dnaddrp = dnet_addr(hostname);
  438.             if (!dnaddrp && (np = getnodebyname (hostname)))
  439.         {
  440.         /* node was specified by name */
  441.         saddr.sa.sa_family = np->n_addrtype;
  442.         len = sizeof(saddr.sa);
  443.         if (ConvertAddr (&saddr.sa, &len, &addr) == AF_DECnet)
  444.         {
  445.             bzero ((char *) &dnaddr, sizeof (dnaddr));
  446.             dnaddr.a_len = np->n_length;
  447.             acopy (np->n_addr, dnaddr.a_addr, np->n_length);
  448.             dnaddrp = &dnaddr;
  449.         }
  450.             }
  451.         if (dnaddrp)
  452.         NewHost((short)AF_DECnet, (pointer)dnaddrp,
  453.             (int)(dnaddrp->a_len + sizeof(dnaddrp->a_len)));
  454.         }
  455.     else
  456.     {
  457. #endif /* DNETCONN */
  458. #ifdef TCPCONN
  459.             /* host name */
  460.             if (hp = gethostbyname (hostname))
  461.         {
  462.             saddr.sa.sa_family = hp->h_addrtype;
  463.         len = sizeof(saddr.sa);
  464.             if ((family = ConvertAddr (&saddr.sa, &len, &addr)) > 0)
  465. #ifdef h_addr                /* new 4.3bsd version of gethostent */
  466.         {
  467.             char **list;
  468.  
  469.             /* iterate over the addresses */
  470.             for (list = hp->h_addr_list; *list; list++)
  471.             NewHost (family, (pointer)*list, len);
  472.         }
  473. #else
  474.                 NewHost (family, (pointer)hp->h_addr, len);
  475. #endif
  476.  
  477.             }
  478. #endif /* TCPCONN */
  479. #ifdef DNETCONN
  480.         }    
  481. #endif /* DNETCONN */
  482.         }
  483.         fclose (fd);
  484.     }
  485. }
  486.  
  487. static Bool
  488. AuthorizedClient(client)
  489.     ClientPtr client;
  490. {
  491.     int            alen, family;
  492.     struct sockaddr    from;
  493.     pointer        addr;
  494.     register HOST    *host;
  495.  
  496. #ifdef SPRITEPDEVCONN
  497.     if (PdevIsPdevConn(((OsCommPtr)client->osPrivate)->fd))
  498.     return TRUE;
  499. #endif
  500.     alen = sizeof (from);
  501.     if (!getpeername (((OsCommPtr)client->osPrivate)->fd, &from, &alen))
  502.     {
  503.         if ((family = ConvertAddr (&from, &alen, &addr)) >= 0)
  504.     {
  505.         if (family == 0)
  506.         return TRUE;
  507.         for (host = selfhosts; host; host = host->next)
  508.         {
  509.         if (addrEqual (family, addr, alen, host))
  510.             return TRUE;
  511.         }
  512.     }
  513.     }
  514.     return FALSE;
  515. }
  516.  
  517. /* Add a host to the access control list.  This is the external interface
  518.  * called from the dispatcher */
  519.  
  520. int
  521. AddHost (client, family, length, pAddr)
  522.     ClientPtr        client;
  523.     int                 family;
  524.     unsigned            length;        /* of bytes in pAddr */
  525.     pointer             pAddr;
  526. {
  527.     int            len;
  528.     register HOST    *host;
  529.     int                 unixFamily;
  530.  
  531.     if (!AuthorizedClient(client))
  532.     return(BadAccess);
  533.     unixFamily = UnixFamily(family);
  534.     if (unixFamily < 0)
  535.     {
  536.     client->errorValue = family;
  537.     return (BadValue);
  538.     }
  539.     if ((len = CheckAddr (unixFamily, pAddr, length)) < 0)
  540.     {
  541.     client->errorValue = length;
  542.         return (BadValue);
  543.     }
  544.     for (host = validhosts; host; host = host->next)
  545.     {
  546.         if (addrEqual (unixFamily, pAddr, len, host))
  547.             return (Success);
  548.     }
  549.     host = (HOST *) xalloc (sizeof (HOST));
  550.     if (!host)
  551.     return(BadAlloc);
  552.     host->family = unixFamily;
  553.     host->len = len;
  554.     acopy(pAddr, host->addr, len);
  555.     host->next = validhosts;
  556.     validhosts = host;
  557.     return (Success);
  558. }
  559.  
  560. /* Add a host to the access control list. This is the internal interface 
  561.  * called when starting or resetting the server */
  562. static void
  563. NewHost (family, addr, len)
  564.     short    family;
  565.     pointer    addr;
  566.     int        len;
  567. {
  568.     register HOST *host;
  569.  
  570.     for (host = validhosts; host; host = host->next)
  571.     {
  572.         if (addrEqual (family, addr, len, host))
  573.         return;
  574.     }
  575.     host = (HOST *) xalloc (sizeof (HOST));
  576.     if (host)
  577.     {
  578.     host->family = family;
  579.     host->len = len;
  580.     acopy(addr, host->addr, len);
  581.     host->next = validhosts;
  582.     validhosts = host;
  583.     }
  584. }
  585.  
  586. /* Remove a host from the access control list */
  587.  
  588. int
  589. RemoveHost (client, family, length, pAddr)
  590.     ClientPtr        client;
  591.     int                 family;
  592.     unsigned            length;        /* of bytes in pAddr */
  593.     pointer             pAddr;
  594. {
  595.     int            len,
  596.                         unixFamily;
  597.     register HOST    *host, **prev;
  598.  
  599.     if (!AuthorizedClient(client))
  600.     return(BadAccess);
  601.     unixFamily = UnixFamily(family);
  602.     if (unixFamily < 0)
  603.     {
  604.     client->errorValue = family;
  605.         return(BadValue);
  606.     }
  607.     if ((len = CheckAddr (unixFamily, pAddr, length)) < 0)
  608.     {
  609.     client->errorValue = length;
  610.         return(BadValue);
  611.     }
  612.     for (prev = &validhosts;
  613.          (host = *prev) && (!addrEqual (unixFamily, pAddr, len, host));
  614.          prev = &host->next)
  615.         ;
  616.     if (host)
  617.     {
  618.         *prev = host->next;
  619.         xfree (host);
  620.     }
  621.     return (Success);
  622. }
  623.  
  624. /* Get all hosts in the access control list */
  625. int
  626. GetHosts (data, pnHosts, pLen, pEnabled)
  627.     pointer        *data;
  628.     int            *pnHosts;
  629.     int            *pLen;
  630.     BOOL        *pEnabled;
  631. {
  632.     int            len;
  633.     register int     n = 0;
  634.     register pointer    ptr;
  635.     register HOST    *host;
  636.     int            nHosts = 0;
  637.     int            *lengths = (int *) NULL;
  638.     int            *newlens;
  639.  
  640.     *pEnabled = AccessEnabled ? EnableAccess : DisableAccess;
  641.     for (host = validhosts; host; host = host->next)
  642.     {
  643.     newlens = (int *) xrealloc(lengths, (nHosts + 1) * sizeof(int));
  644.     if (!newlens)
  645.     {
  646.         xfree(lengths);
  647.         return(BadAlloc);
  648.     }
  649.     lengths = newlens;
  650.     lengths[nHosts++] = host->len;
  651.     n += (((host->len + 3) >> 2) << 2) + sizeof(xHostEntry);
  652.     }
  653.     if (n)
  654.     {
  655.         *data = ptr = (pointer) xalloc (n);
  656.     if (!ptr)
  657.     {
  658.         xfree(lengths);
  659.         return(BadAlloc);
  660.     }
  661.     nHosts = 0;
  662.         for (host = validhosts; host; host = host->next)
  663.     {
  664.  
  665.         len = lengths[nHosts++];
  666.         ((xHostEntry *)ptr)->family = XFamily(host->family);
  667.         ((xHostEntry *)ptr)->length = len;
  668.         ptr += sizeof(xHostEntry);
  669.         acopy (host->addr, ptr, len);
  670.         ptr += ((len + 3) >> 2) << 2;
  671.         }
  672.     } else {
  673.     *data = NULL;
  674.     }
  675.     *pnHosts = nHosts;
  676.     *pLen = n;
  677.     xfree(lengths);
  678.     return(Success);
  679. }
  680.  
  681. /* Check for valid address family and length, and return address length. */
  682.  
  683. /*ARGSUSED*/
  684. static int
  685. CheckAddr (family, pAddr, length)
  686.     int            family;
  687.     pointer        pAddr;
  688.     unsigned        length;
  689. {
  690.     int    len;
  691.  
  692.     switch (family)
  693.     {
  694. #ifdef TCPCONN
  695.       case AF_INET:
  696.     if (length == sizeof (struct in_addr))
  697.         len = length;
  698.     else
  699.         len = -1;
  700.         break;
  701. #endif 
  702. #ifdef DNETCONN
  703.       case AF_DECnet:
  704.         {
  705.         struct dn_naddr *dnaddr = (struct dn_naddr *) pAddr;
  706.  
  707.         if ((length < sizeof(dnaddr->a_len)) ||
  708.         (length < dnaddr->a_len + sizeof(dnaddr->a_len)))
  709.         len = -1;
  710.         else
  711.         len = dnaddr->a_len + sizeof(dnaddr->a_len);
  712.         if (len > sizeof(struct dn_naddr))
  713.         len = -1;
  714.     }
  715.         break;
  716. #endif
  717.       default:
  718.         len = -1;
  719.     }
  720.     return (len);
  721. }
  722.  
  723. /* Check if a host is not in the access control list. 
  724.  * Returns 1 if host is invalid, 0 if we've found it. */
  725.  
  726. InvalidHost (saddr, len)
  727.     register struct sockaddr    *saddr;
  728.     int                len;
  729. {
  730.     int             family;
  731.     pointer            addr;
  732.     register HOST         *selfhost, *host;
  733.  
  734.     if (!AccessEnabled)   /* just let them in */
  735.         return(0);    
  736.     if ((family = ConvertAddr (saddr, &len, &addr)) < 0)
  737.         return (1);
  738.     if (family == 0)
  739.     {
  740.     if (!LocalHostEnabled)
  741.      {
  742.         /*
  743.          * check to see if any local address is enabled.  This 
  744.          * implicitly enables local connections.
  745.          */
  746.         for (selfhost = selfhosts; selfhost; selfhost=selfhost->next)
  747.          {
  748.         for (host = validhosts; host; host=host->next)
  749.         {
  750.             if (addrEqual (selfhost->family, selfhost->addr,
  751.                    selfhost->len, host))
  752.             return 0;
  753.         }
  754.         }
  755.         return 1;
  756.     } else
  757.         return (0);
  758.     }
  759.     for (host = validhosts; host; host = host->next)
  760.     {
  761.         if (addrEqual (family, addr, len, host))
  762.             return (0);
  763.     }
  764.     return (1);
  765. }
  766.  
  767. #ifdef SPRITEPDEVCONN
  768. /* Check whether this is an allowed pdev connection.
  769.   This is called from ClientAuthorized() in connection.c.
  770.   We have to do this here because result will depend on LocalHostEnabled. */
  771. InvalidPdevConn (fd)
  772.     int    fd;        /* connection */
  773. {
  774.     if (!PdevIsPdevConn(fd))
  775.     return 1;        /* no pdev, no cigar */
  776.     else
  777.     /*
  778.      * Sprite is like one big host for authorization
  779.      * purposes, so we allow pdev connections (from any
  780.      * Sprite host) iff LocalHostEnabled.
  781.      */
  782.     return !LocalHostEnabled;
  783. }
  784. #endif /* SPRITEPDEVCONN */
  785.  
  786. static int
  787. ConvertAddr (saddr, len, addr)
  788.     register struct sockaddr    *saddr;
  789.     int                *len;
  790.     pointer            *addr;
  791. {
  792.     if (*len == 0)
  793.         return (0);
  794.     switch (saddr->sa_family)
  795.     {
  796.       case AF_UNSPEC:
  797. #ifdef UNIXCONN
  798.       case AF_UNIX:
  799. #endif
  800.         return (0);
  801.  
  802. #ifdef TCPCONN
  803.       case AF_INET:
  804.         *len = sizeof (struct in_addr);
  805.         *addr = (pointer) &(((struct sockaddr_in *) saddr)->sin_addr);
  806.         return (AF_INET);
  807. #endif
  808.  
  809. #ifdef DNETCONN
  810.       case AF_DECnet:
  811.     {
  812.         struct sockaddr_dn *sdn = (struct sockaddr_dn *) saddr;
  813.         *len = sdn->sdn_nodeaddrl + sizeof(sdn->sdn_nodeaddrl);
  814.         *addr = (pointer) &(sdn->sdn_add);
  815.     }
  816.         return (AF_DECnet);
  817. #endif
  818.  
  819.       default:
  820.         break;
  821.     }
  822.     return (-1);
  823. }
  824.  
  825. int
  826. ChangeAccessControl(client, fEnabled)
  827.     ClientPtr client;
  828.     int fEnabled;
  829. {
  830.     if (!AuthorizedClient(client))
  831.     return BadAccess;
  832.     AccessEnabled = fEnabled;
  833.     return Success;
  834. }
  835.  
  836. static int XFamily(af)
  837.     int af;
  838. {
  839.     int i;
  840.     for (i = 0; i < FAMILIES; i++)
  841.         if (familyMap[i].af == af)
  842.             return familyMap[i].xf;
  843.     return -1;
  844. }
  845.  
  846. static int UnixFamily(xf)
  847.     int xf;
  848. {
  849.     int i;
  850.     for (i = 0; i < FAMILIES; i++)
  851.         if (familyMap[i].xf == xf)
  852.             return familyMap[i].af;
  853.     return -1;
  854. }
  855.  
  856. @
  857.  
  858.  
  859. 1.1
  860. log
  861. @Initial revision
  862. @
  863. text
  864. @d467 4
  865. d737 19
  866. @
  867.